package com.cyclecoder.demo.listener;

import com.cyclecoder.demo.event.WeatherEvent;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractEventMulticaster implements EventMulticaster {
    private List<WeatherListener> weatherListeners = new ArrayList<>();

    @Override
    public void addListener(WeatherListener weatherListener) {
        this.weatherListeners.add(weatherListener);
    }

    @Override
    public void removeListener(WeatherListener weatherListener) {
        this.weatherListeners.remove(weatherListener);
    }

    @Override
    public void multicastEvent(WeatherEvent weatherEvent) {
        doStart();
        weatherListeners.forEach(event -> event.onWeather(weatherEvent));
        doEnd();
    }

    protected abstract void doStart();

    protected abstract void doEnd();
}
